﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QLPKWeb.Models.LinQ.LichLamViec;
using Webdiyer.WebControls.Mvc;
using System.Web.Mvc;
using QLPKWeb.Models.LinQ.Common;

namespace QLPKWeb.Models.FormBean.LichLamViecModel
{
    public class LichLamViecForm
    {
        public int ID { get; set; }
        public string StartTimeHH { get; set; }
        public string StartTimeMM { get; set; }
        public string EndTimeHH { get; set; }
        public string EndTimeMM { get; set; }
        public string EventDate { get; set; }
        public string EndDate { get; set; }
        public string NoiDung { get; set; }
        public string DiaDiem { get; set; }
        public string KhachMoi { get; set; }
        public string DonViChuTri { get; set; }
        public string GhiChu { get; set; }
        public bool? Status { get; set; }
        public int? NguoiTao { get; set; }
        public DateTime? NgayTao { get; set; }
        // các list
        public PagedList<LichLamViec> LichLamViecPagedList { get; set; }
        public SelectList ComboboxLoai { get; set; }
        public List<TB_User> ListUserNotInDepartment { get; set; }
        public List<TB_User> ListUser { get; set; }
        public List<TB_User> ListDisplayUser { get; set; }
        public List<TB_Department> ListPhongBan { get; set; }
        public List<LichLamViecShared> ListThanhPhanThamDu { get; set; }
        public List<LichLamViec> ListLichChuaDuyet { get; set; }
        public List<LichLamViec> ListLichDaDuyet { get; set; }
        // các biến phụ
        public int PageNumber { get; set; }
        public bool IsView { get; set; }
        public string FromDate { get; set; }
        public string ToDate { get; set; }
        public string InListTextThanhPhan { get; set; }
        public string InListTextNguoiChuanBi { get; set; }
        public string InListIdThanhPhan { get; set; }
        public string InListIdNguoiChuanBi { get; set; }
        public string InListIdLich { get; set; }
        public string inListIdChuaDuyet { get; set; }
        public string inListIdDuyet { get; set; }
        // các biến message
        public string MessageCreateLichLamViec { get; set; }
        public string MessageEditLichLamViec { get; set; }
        // các hàm hổ trợ
        public List<TB_User> GetUserByDepartment(List<TB_User> list, int department_id)
        {
            List<TB_User> returntList = new List<TB_User>();
            foreach (TB_User item in list)
            {
                if (item.Department == department_id)
                    returntList.Add(item);
            }
            return returntList;
        }
        public bool CheckInThanhPhanThamDu(int IdUser)
        {
            if (ListThanhPhanThamDu == null || ListUser == null)
                return false;
            else
            {
                foreach (LichLamViecShared item in ListThanhPhanThamDu)
                {
                    if (item.IdUser == IdUser)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        public bool CheckCheckboxIsFull(int iddepartment)
        {
            return false;
        }
        public string GetDisplayNameOfuserById(int Id)
        {
            foreach (TB_User item in ListUser)
            {
                if (item.ID == Id)
                    return ((string.IsNullOrEmpty(item.LastName) || string.IsNullOrEmpty(item.FirstName))
                                      ? item.UserName
                                      : item.LastName + " " + item.FirstName);
            }
            return "";
        }
        public string GetDisplayNameOfThanhPhan(List<LichLamViecShared> ListThanhPhan)
        {
            string display = "";
            if (ListThanhPhan.Count < 3)
                foreach (LichLamViecShared item in ListThanhPhan)
                {
                    display += this.GetDisplayNameOfuserById(item.IdUser.Value) + ", ";
                }
            else
            {
                for (int i = 0; i < 2; i++)
                {
                    display += this.GetDisplayNameOfuserById(ListThanhPhan[i].IdUser.Value) + ", ";
                }
                display += "<a id='XemThanhPhan" + ListThanhPhan[0].IDLich + "' class='XemThanhPhan'>Xem hết</a>";
            }
            if (!string.IsNullOrEmpty(display))
                display = display.Substring(0, display.Length - 2);
            return display;
        }
        public string GetDisplayNameOfThanhPhan(List<LichLamViecShared> ListThanhPhan, int songuoi)
        {
            string display = "";
            if (ListThanhPhan.Count < songuoi)
            {
                foreach (LichLamViecShared item in ListThanhPhan)
                {
                    display += this.GetDisplayNameOfuserById(item.IdUser.Value) + ", ";
                }
                    display = display.Substring(0, display.Length - 2);
            }
            else
            {
                for (int i = 0; i < songuoi; i++)
                {
                    display += this.GetDisplayNameOfuserById(ListThanhPhan[i].IdUser.Value) + ", ";
                }
                    display = display.Substring(0, display.Length - 2);
                display += " <a id='XemThanhPhan" + ListThanhPhan[0].IDLich + "' class='XemThanhPhan'>Xem hết</a>";
            }
            return display;
        }
        public string GetDisplayNameOfNguoiChuanBi(List<LichLamViec_NguoiChuanBi> ListNguoiChuanBi)
        {
            string display = "";
            if (ListNguoiChuanBi.Count < 3)
                foreach (LichLamViec_NguoiChuanBi item in ListNguoiChuanBi)
                {
                    display += this.GetDisplayNameOfuserById(item.IdUser) + ", ";
                }
            else
            {
                for (int i = 0; i < 2; i++)
                {
                    display += this.GetDisplayNameOfuserById(ListNguoiChuanBi[i].IdUser) + ", ";
                }
                display += "<a id='XemNguoiChuanBi" + ListNguoiChuanBi[0].IDLich + "' class='XemNguoiChuanBi'>Xem hết</a>";
            }
            if (!string.IsNullOrEmpty(display))
                display = display.Substring(0, display.Length - 2);
            return display;
        }
        public bool CheckUserInlist(List<LichLamViecShared> ListThanhPhan, int IdUser)
        {
            foreach (LichLamViecShared item in ListThanhPhan)
            {
                if (item.IdUser == IdUser) return true;
            }
            return false;
        }
        public string CheckDayIsInListLichLamViec(int DayOfWeek, int IdUser)
        {
            string display = "";
            foreach (LichLamViec item in ListLichDaDuyet)
            {
                DateTime eventdate = item.StartTime != null ? item.StartTime.Value : DateTime.Now;
                int day = (int)eventdate.DayOfWeek;
                if (day == DayOfWeek && CheckUserInlist(item.LichLamViecShareds.ToList(), IdUser))
                    display = "<a onclick=\"javascript:window.location.href = basePath + 'LichLamViec/XemLichLamViec/" + item.ID + "'\"> - "
                        + item.NoiDung + "</a>";
            }
            return display;
        }

        //public List<LichLamViec> GetListLichByDayOfWeek(List<LichLamViec> lst, int dayofweek)
        //{
        //    return lst.Where(p => (int)p.EventDate.Value.DayOfWeek == dayofweek).ToList();
        //}
    }
}